package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.rpc.model.RobotStatusManager;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.cache.MemoryCacheUtil;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.sfrobot.entity.RobotError;
import com.lizhi.yun.db.sfrobot.entity.RobotStatus;
import com.lizhi.yun.db.sfrobot.repository.RobotStatusRepo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/11/18 10:16
 */
@Slf4j
@Service
public class RecvRobotStatusHandler {

    @Resource
    private RobotStatusRepo robotStatusRepo;

    public String recvRobotStatus(String json){
        UpDataPOJO upDataPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upDataPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        log.info("获取到上传的信息是:{}", upDataPOJO.getData());

        RobotStatus request = SamJsonUtil.toObject(upDataPOJO.getData(), RobotStatus.class);
        if (StringUtils.isEmpty(request)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        /*RobotStatus example = new RobotStatus();
        example.setRobotDeviceId(request.getRobotDeviceId());
        example.setHospitalId(request.getHospitalId());
        example.setHospitalSystemId(request.getHospitalSystemId());
        RobotStatus recordFromDb = robotStatusRepo.findOne(Example.of(example)).orElse(null);
        if (StringUtils.isEmpty(recordFromDb)){
            request.setLid(StringUtils.newId());
        } else {
            request.setLid(recordFromDb.lid);
        }*/
        request.setLid(StringUtils.newId());
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        request.setHospitalId(rpcClientPOJO.getOrganId());
        request.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        /*数据上传的时间*/
        request.setDataUpTime(DateUtils.getNowDate());
        log.info("获取到的信息request:{}", request);
//        robotStatusRepo.save(request);
        RobotStatusManager.getInstance().updateStatus(request);
        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
